<?php
class Modelo_categorias extends Model {

    public $datos = array();
    public $html;
    public $categoria_nombre;
    public $category_flypage;

    public $desc;
    public $desc_english;
    public $desc_italy;
    public $desc_portugues;
    

    function Modelo_categorias() {
        parent::Model();
    }


    /**
     * Llena variables con datos de la categoria sin seo
     */
    function get_categorias_datos($id) {
        $query = $this->db->query("SELECT * FROM category WHERE category_id='".$id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $this->categoria_nombre = $row->category_name;
        $this->category_flypage = $row->category_flypage;

        $query = $this->db->query("SELECT * FROM category_text WHERE category_id='".$row->category_id."'");
        foreach ($query->result() as $row) {
            if($row->idioma == 'spanish') {
                $this->desc   = $row->category_desc;
            }elseif($row->idioma == 'english') {
                $this->desc_english   = $row->category_desc;
            }elseif($row->idioma == 'italy') {
                $this->desc_italy   = $row->category_desc;
            }elseif($row->idioma == 'portugues') {
                $this->desc_portugues   = $row->category_desc;
            }
        }

    }

     /**
     * Llena variables con datos de la categoria con seo
     */
    function get_categorias_datos_seo($id) {
        //Agregado SEO
        $query = $this->db->query("SELECT category_id FROM category WHERE category_flypage='".$id."'");
        $row1=$query->row();//$row1->category_id

        $query = $this->db->query("SELECT * FROM category WHERE category_id='".$row1->category_id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $this->categoria_nombre = $row->category_name;
        $this->category_flypage = $row->category_flypage;

        $query = $this->db->query("SELECT * FROM category_text WHERE category_id='".$row1->category_id."'");
        foreach ($query->result() as $row) {
            if($row->idioma == 'spanish') {
                $this->desc   = $row->category_desc;
            }elseif($row->idioma == 'english') {
                $this->desc_english   = $row->category_desc;
            }elseif($row->idioma == 'italy') {
                $this->desc_italy   = $row->category_desc;
            }elseif($row->idioma == 'portugues') {
                $this->desc_portugues   = $row->category_desc;
            }
        }

    }



    /**
     * Retorna datos listado de categorias
     */
    function get_categorias() {
        $query['consulta2'] = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id = 0 ORDER BY category_child_id");
        return $query;
    }

    /**
     * Retorna datos listado de productos pertenecientes a una categoria
     */
    function get_productos_categorias($id) { //pasar a modelo categorias
        $this->datos = $this->db->query("SELECT * FROM product_category_xref WHERE category_id =$id");
    }

    /**
     * Retorna datos listado de productos pertenecientes a una categoria con SEO
     */
    function get_productos_categorias_seo($id) { //pasar a modelo categorias
        //Agregado SEO
        $query = $this->db->query("SELECT category_id FROM category WHERE category_flypage='".$id."'");
        $row1=$query->row();
        $this->datos = $this->db->query("SELECT * FROM product_category_xref WHERE category_id='".$row1->category_id."'");
    }

    /**
     * Retorna datos listado de productos pertenecientes a una categoria con SEO
     */
    function get_productos_categorias_buscador($term) { //pasar a modelo categorias

        $sql  = "select distinct product_id from product_text where ";
        $sql .= "product_text.product_s_desc like '%$term%' or product_text.product_desc like '%$term%'";

        $this->datos = $this->db->query($sql);
    }


    /**
     * Devuelve array con listado de categorias padre
     */
    function get_categorias_div() { //pasar a modelo categorias
        $consulta2 = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id = 0");
        $html = '';
        $html .='<table border="0">';
        foreach ($consulta2->result() as $row) {
            $html .='<tr>';
            $query = $this->db->query("SELECT category_id, category_flypage FROM category WHERE category_id=".$row->category_child_id);
            $row2=$query->row();
            $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id=".$row2->category_id." AND idioma='".$this->session->userdata('lenguaje')."'");
            $row3=$query->row();
            $html .='<td><a href="'.base_url().'index.php/ec/sub_categoria/'.$row2->category_flypage.'">'.$row3->category_desc.'</a></td>';
            $html .='</tr>';
        }
        $html .='</table>';
        $this->html = $html;
    }

    /**
     * Devuelve array con listado de sub_categorias
     */
    function get_lista_sub_categorias($id) { //pasar a modelo categorias
        //Agregado SEO
        $query = $this->db->query("SELECT category_id FROM category WHERE category_flypage='".$id."'");
        $row1=$query->row();//$row1->category_id

        $consulta2 = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id =$row1->category_id");
        $row=$consulta2->row();

        foreach ($consulta2->result() as $row) {
            $query = $this->db->query("SELECT category_id FROM category WHERE category_id=".$row->category_child_id);
            $row2=$query->row();
            $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id=".$row2->category_id." AND idioma='".$this->session->userdata('lenguaje')."'");
            $row3=$query->row();
            $this->lista_sub_categorias[$row2->category_id]=$row3->category_desc;
        }
    }

    /**
     * Retorna nombre de la categoria segun id del producto sin SEO
     */
    function get_nombre_categoria($id) {
        $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id='".$id."'");
        echo "SELECT category_desc FROM category_text WHERE category_id='".$id."'";
        $row3=$query->row();
        $this->categoria_nombre = $row3->category_desc;
    }


      /**
     * Retorna nombre de la categoria segun id del producto con SEO
     */
    function get_nombre_categoria_seo($id) {
        //Agregado SEO
        $query = $this->db->query("SELECT category_id FROM category WHERE category_flypage='".$id."'");
        $row1=$query->row();

        $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id='".$row1->category_id."'");
        //echo "SELECT category_desc FROM category_text WHERE category_id='".$id."'";
        $row3=$query->row();
        $this->categoria_nombre = $row3->category_desc;
    }


    /**
     * Retorna nombre de la categoria segun id del producto
     */
    function get_nombre_categoria_bn($id) {
        $query = $this->db->query("SELECT category_id FROM product_category_xref WHERE product_id=".$id);
        $row2=$query->row();
        $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id='".$row2->category_id."'");
        $row3=$query->row();
        $this->categoria_nombre = $row3->category_desc;
    }

    /**
     * Retorna nombre de la categoria segun id del producto con SEO
     */
    function get_nombre_categoria_bn_seo($id) {
        //Agregado SEO
        $query = $this->db->query("SELECT product_id FROM product WHERE product_desc='".$id."'");
        $row1=$query->row();
        $id=$row1->product_id;

//Agregado SEO
//        $query = $this->db->query("SELECT category_id FROM category WHERE category_flypage='".$id."'");
//        $row1=$query->row();

        $query = $this->db->query("SELECT category_id FROM product_category_xref WHERE product_id='".$id."'");
        $row2=$query->row();
        $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id='".$row2->category_id."'");
        $row3=$query->row();
        $this->categoria_nombre = $row3->category_desc;
    }



    /**
     * Retorna nombre de la categoria
     */
    function get_id_categoria_padre($id) {
        //Agregado SEO
        $query = $this->db->query("SELECT category_id FROM category WHERE category_flypage='".$id."'");
        $row1=$query->row();//$row1->category_id

        $query = $this->db->query("SELECT category_parent_id FROM category_xref WHERE category_child_id='".$row1->category_id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $this->category_parent_id = $row->category_parent_id;
        return $this->category_parent_id;
    }

}

?>
